#include "coin.h"
#include <QDebug>

Coin::Coin(QWidget *parent) : QPushButton(parent)
{

}

Coin::Coin(QString normalPath)
{
    this->normalPath = normalPath;
    QPixmap pixmap;
    bool ret = pixmap.load(normalPath);
    if (!ret) {
        qDebug() << "load pixmap failed";
    }
    this->setFixedSize(pixmap.width(), pixmap.height());
    this->setStyleSheet("QPushButton { border:0px }");
    this->setIcon(pixmap);
    this->setIconSize(QSize(pixmap.width(), pixmap.height()));

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [=](){
        QPixmap pix;
        QString str;
        if (!flag) {
            str = QString(":/Image/Coin000%1.png").arg(this->min++);
        } else {
            str = QString(":/Image/Coin000%1.png").arg(this->max--);
        }

        pix.load(str);
        this->setFixedSize(pix.width(), pix.height());
        this->setStyleSheet("QPushButton { border:0px }");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(), pix.height()));
        if (this->min == 9) {
            this->min = 1;
            this->isAnimation = false;
            this->timer->stop();
        } else if (this->max == 0) {
            this->max = 8;
            this->isAnimation = false;
            this->timer->stop();
        }

        /*qDebug() << "min = " << min << "max = " << max;*/
    });
}

void Coin::mousePressEvent(QMouseEvent *e)
{
    if (this->isAnimation || this->isWin) {
        return;
    } else {
        return QPushButton::mousePressEvent(e);
    }
}

void Coin::changeFlag(void)
{
    this->isAnimation = true;
    flag = !flag;
    this->timer->start(30);
}
